package com.agile.major.web.rest;

import com.agile.major.domain.ProductionLearningCooperation;
import com.agile.major.service.ProductionLearningCooperationService;
import com.agile.major.web.rest.util.HeaderUtil;
import com.agile.major.web.rest.util.PaginationUtil;
import com.codahale.metrics.annotation.Timed;
import io.github.jhipster.web.util.ResponseUtil;
import io.swagger.annotations.ApiParam;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.net.URI;
import java.net.URISyntaxException;
import java.util.List;
import java.util.Optional;

/**
 * REST controller for managing ProductionLearningCooperation.
 */
@RestController
@RequestMapping("/api")
public class ProductionLearningCooperationResource {

    private final Logger log = LoggerFactory.getLogger(ProductionLearningCooperationResource.class);

    private static final String ENTITY_NAME = "productionLearningCooperation";

    private final ProductionLearningCooperationService productionLearningCooperationService;

    public ProductionLearningCooperationResource(ProductionLearningCooperationService productionLearningCooperationService) {
        this.productionLearningCooperationService = productionLearningCooperationService;
    }

    /**
     * POST  /production-learning-cooperations : Create a new productionLearningCooperation.
     *
     * @param productionLearningCooperation the productionLearningCooperation to create
     * @return the ResponseEntity with status 201 (Created) and with body the new productionLearningCooperation, or with status 400 (Bad Request) if the productionLearningCooperation has already an ID
     * @throws URISyntaxException if the Location URI syntax is incorrect
     */
    @PostMapping("/production-learning-cooperations")
    @Timed
    public ResponseEntity<ProductionLearningCooperation> createProductionLearningCooperation(@RequestBody ProductionLearningCooperation productionLearningCooperation) throws URISyntaxException {
        log.debug("REST request to save ProductionLearningCooperation : {}", productionLearningCooperation);
        if (productionLearningCooperation.getId() != null) {
            return ResponseEntity.badRequest().headers(HeaderUtil.createFailureAlert(ENTITY_NAME, "idexists", "A new productionLearningCooperation cannot already have an ID")).body(null);
        }
        ProductionLearningCooperation result = productionLearningCooperationService.save(productionLearningCooperation);
        return ResponseEntity.created(new URI("/api/production-learning-cooperations/" + result.getId()))
            .headers(HeaderUtil.createEntityCreationAlert(ENTITY_NAME, result.getId().toString()))
            .body(result);
    }

    /**
     * PUT  /production-learning-cooperations : Updates an existing productionLearningCooperation.
     *
     * @param productionLearningCooperation the productionLearningCooperation to update
     * @return the ResponseEntity with status 200 (OK) and with body the updated productionLearningCooperation,
     * or with status 400 (Bad Request) if the productionLearningCooperation is not valid,
     * or with status 500 (Internal Server Error) if the productionLearningCooperation couldn't be updated
     * @throws URISyntaxException if the Location URI syntax is incorrect
     */
    @PutMapping("/production-learning-cooperations")
    @Timed
    public ResponseEntity<ProductionLearningCooperation> updateProductionLearningCooperation(@RequestBody ProductionLearningCooperation productionLearningCooperation) throws URISyntaxException {
        log.debug("REST request to update ProductionLearningCooperation : {}", productionLearningCooperation);
        if (productionLearningCooperation.getId() == null) {
            return createProductionLearningCooperation(productionLearningCooperation);
        }
        ProductionLearningCooperation result = productionLearningCooperationService.save(productionLearningCooperation);
        return ResponseEntity.ok()
            .headers(HeaderUtil.createEntityUpdateAlert(ENTITY_NAME, productionLearningCooperation.getId().toString()))
            .body(result);
    }

    /**
     * GET  /production-learning-cooperations : get all the productionLearningCooperations.
     *
     * @param pageable the pagination information
     * @return the ResponseEntity with status 200 (OK) and the list of productionLearningCooperations in body
     */
    @GetMapping("/production-learning-cooperations")
    @Timed
    public ResponseEntity<List<ProductionLearningCooperation>> getAllProductionLearningCooperations(@ApiParam Pageable pageable) {
        log.debug("REST request to get a page of ProductionLearningCooperations");
        Page<ProductionLearningCooperation> page = productionLearningCooperationService.findAll(pageable);
        HttpHeaders headers = PaginationUtil.generatePaginationHttpHeaders(page, "/api/production-learning-cooperations");
        return new ResponseEntity<>(page.getContent(), headers, HttpStatus.OK);
    }

    /**
     * GET  /production-learning-cooperations/:id : get the "id" productionLearningCooperation.
     *
     * @param id the id of the productionLearningCooperation to retrieve
     * @return the ResponseEntity with status 200 (OK) and with body the productionLearningCooperation, or with status 404 (Not Found)
     */
    @GetMapping("/production-learning-cooperations/{id}")
    @Timed
    public ResponseEntity<ProductionLearningCooperation> getProductionLearningCooperation(@PathVariable Long id) {
        log.debug("REST request to get ProductionLearningCooperation : {}", id);
        ProductionLearningCooperation productionLearningCooperation = productionLearningCooperationService.findOne(id);
        return ResponseUtil.wrapOrNotFound(Optional.ofNullable(productionLearningCooperation));
    }

    /**
     * DELETE  /production-learning-cooperations/:id : delete the "id" productionLearningCooperation.
     *
     * @param id the id of the productionLearningCooperation to delete
     * @return the ResponseEntity with status 200 (OK)
     */
    @DeleteMapping("/production-learning-cooperations/{id}")
    @Timed
    public ResponseEntity<Void> deleteProductionLearningCooperation(@PathVariable Long id) {
        log.debug("REST request to delete ProductionLearningCooperation : {}", id);
        productionLearningCooperationService.delete(id);
        return ResponseEntity.ok().headers(HeaderUtil.createEntityDeletionAlert(ENTITY_NAME, id.toString())).build();
    }
}
